package cn.msi.contract.service;

import java.math.BigDecimal;
import java.util.Date;

import cn.common.manager.SessionManager;
import cn.common.util.GeneralUtils;
import cn.msi.contract.domain.Contract;
import cn.msi.contract.domain.ContractChange;
import cn.msi.contract.mapper.ContractChangeMapper;
import cn.msi.contract.mapper.ContractMapper;
import cn.msi.customer.domain.User;

/**
 * 合同业务处理类
 * 
 * @author Administrator
 * 
 */
public class ContractService {

	private ContractMapper contractMapper;

	private ContractChangeMapper contractChangeMapper;

	/**
	 * 合同查询业务处理方法
	 * 
	 * @param contract
	 * @return
	 */
	public String searchContract(Contract contract) {
		User user = (User) SessionManager.getAttr("user");
		contract.setCustId(user.getCustId());
		return GeneralUtils.easyUI(contractMapper.count(contract), contractMapper.selectList(contract));
	}

	/**
	 * 作废合同查询业务处理方法
	 * 
	 * @param contract
	 * @return
	 */
	public String searchChannelContract(Contract contract) {
		User user = (User) SessionManager.getAttr("user");
		contract.setCustId(user.getCustId());
		return GeneralUtils.easyUI(contractMapper.countChannel(contract), contractMapper.selectChannelList(contract));
	}

	/**
	 * 查找合同
	 * 
	 * @param contract
	 * @return
	 */
	public Contract findContract(Contract contract) {
		if (contract.getContractId() != null) {
			contract = contractMapper.selectById(contract.getContractId());
		}
		return contract;
	}

	/**
	 * 新增用户合同
	 * 
	 * @param contract
	 */
	public void addContract(Contract contract) {
		User user = (User) SessionManager.getAttr("user");
		contract.setCustId(user.getCustId());
		contract.setCreateTime(new Date());
		contract.setAutFlag("N");
		contract.setContractVersion("1.0");
		contract.setContractState("P");
		contractMapper.insert(contract);
	}

	/**
	 * 合同审核
	 * 
	 * @param contract
	 */
	public void autContract(Contract contract) {
		contract.setAutFlag("Y");
		contractMapper.update(contract);
	}

	/**
	 * 合同作废
	 * 
	 * @param contract
	 */
	public void channelContract(Contract contract) {
		contract.setContractState("D");
		contractMapper.update(contract);
	}

	/**
	 * 保存合同变更历史记录
	 * 
	 * @param contract
	 * @param contractChange
	 */
	public void addContractChange(Contract contract, ContractChange contractChange) {
		// 获得合同信息,并保存
		Contract c = contractMapper.selectById(contract.getContractId());
		// 合同版本号
		//Double version = Double.parseDouble(c.getContractVersion());
		BigDecimal version = new BigDecimal(c.getContractVersion());
		c.setContractId(null);
		c.setHisId(contract.getContractId());
		c.setContractState("H");
		contractMapper.insert(c);
		// 更改合同信息
		version = (version.add(new BigDecimal(0.1))).setScale(1, BigDecimal.ROUND_HALF_UP);
		contract.setContractVersion(String.valueOf(version));
		contract.setContractState("P");
		contractMapper.update(contract);
		// 保存合同变更记录
		User user = (User) SessionManager.getAttr("user");
		contractChange.setContractId(c.getContractId());
		contractChange.setUserId(user.getUserId());
		contractChange.setChangeDate(new Date());
		contractChangeMapper.insert(contractChange);
	}
	
	/**
	 * 合同变更历史列表
	 * @param contractChange
	 * @return
	 */
	public String searchContractHisList(ContractChange contractChange) {
		return GeneralUtils.easyUI(contractChangeMapper.count(contractChange), contractChangeMapper.selectList(contractChange));
	}

	public void setContractMapper(ContractMapper contractMapper) {
		this.contractMapper = contractMapper;
	}

	public void setContractChangeMapper(ContractChangeMapper contractChangeMapper) {
		this.contractChangeMapper = contractChangeMapper;
	}
}
